package mod.luckymod.WorldGen;

import mod.luckymod.Luckymod;
import mod.luckymod.ModBlocks;
import net.minecraft.core.registries.Registries;
import net.minecraft.data.worldgen.BootstapContext;
import net.minecraft.resources.ResourceKey;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.level.levelgen.feature.ConfiguredFeature;
import net.minecraft.world.level.levelgen.feature.Feature;
import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration;
import net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration;
import net.minecraft.world.level.levelgen.structure.templatesystem.RuleTest;
import net.minecraft.world.level.levelgen.structure.templatesystem.TagMatchTest;

import java.util.List;

public class ModConfiguredFeatures {
    public static final ResourceKey<ConfiguredFeature<?, ?>> OVERWORLD_SODIUM_ORE = registerKey("sodium_ore");
    public static void bootstrap(BootstapContext<ConfiguredFeature<?, ?>> context) {
        RuleTest naturalStoneReplaceable = new TagMatchTest(BlockTags.STONE_ORE_REPLACEABLES); // 替换石头
        RuleTest deepslateReplaceable = new TagMatchTest(BlockTags.DEEPSLATE_ORE_REPLACEABLES); // 替换深板岩

        // 定义两个规则测试，分别用于石头和深板岩环境
        List<OreConfiguration.TargetBlockState> targetList = List.of(
                OreConfiguration.target(naturalStoneReplaceable, ModBlocks.sodium_ORE.get().defaultBlockState()),
                OreConfiguration.target(deepslateReplaceable, ModBlocks.sodium_ORE.get().defaultBlockState())
        );
        // 注册ConfiguredFeature
        register(context, OVERWORLD_SODIUM_ORE, Feature.ORE, new OreConfiguration(targetList, 4, 0.5F)); // 矿脉大小4，禁用歧视（石头和深板岩都会生成）
    }
    public static ResourceKey<ConfiguredFeature<?, ?>> registerKey(String name) {
        return ResourceKey.create(Registries.CONFIGURED_FEATURE, Luckymod.asResource(name));
    }
    private static <FC extends FeatureConfiguration, F extends Feature<FC>> void register(BootstapContext<ConfiguredFeature<?, ?>> context,
                                                                                          ResourceKey<ConfiguredFeature<?, ?>> key, F feature, FC configuration) {
        context.register(key, new ConfiguredFeature<>(feature, configuration));
    }
}
